import os
import json
print("失败")
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

def json_to_srt(json_file_path):
    # 获取文件名（不含扩展名）
    file_name = os.path.splitext(os.path.basename(json_file_path))[0]
    srt_file_path = f"{file_name}.srt"

    # 读取JSON文件
    with open(os.path.join(BASE_DIR, json_file_path), 'r', encoding='utf-8') as file:
        json_data = json.load(file)

    # 初始化字幕编号和时间
    subtitle_number = 1
    start_times = []
    end_times = []
    text_contents = []

    # 提取字幕信息
    for item in json_data:
        if 'words' in item:
            start_times.append(item['start_time'])
            end_times.append(item['end_time'])
            text_contents.append(' '.join(item['words']))

    # 写入SRT文件
    with open(os.path.join(BASE_DIR, srt_file_path), 'w', encoding='utf-8') as file:
        for i in range(len(start_times)):
            start_time = start_times[i]
            end_time = end_times[i]
            text_content = text_contents[i]
            file.write(f"{subtitle_number}\n{start_time} --> {end_time}\n{text_content}\n\n")
            subtitle_number += 1

    print(f"已将JSON文件转换为SRT文件: {os.path.join(BASE_DIR, srt_file_path)}")


# 示例用法
json_file_path = './draft_content.json'

print(os.path.join(BASE_DIR, json_file_path))

json_to_srt(json_file_path)